package com.example.demo.core.service;

import com.example.demo.core.entity.RolePermission;
import com.example.demo.core.repository.RolePermissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RolePermissionService {

    @Autowired
    private RolePermissionRepository rolePermissionRepository;

    public List<RolePermission> getAllPermissionsByRoleId(Integer id){
        return rolePermissionRepository.findAllByRoleId(id);
    }

    /**
    * @Description: 添加角色权限
    * @Param:  roleId: 角色ID
     * @Param: permissionId: 权限ID
    * @return:
    * @Author: wangze
    */
    public void addRolePermission(Integer roleId, Integer permissionId) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionId);
        rolePermissionRepository.save(rolePermission);
    }

    /**
    * @Description: 根据角色ID删除对应角色的所有权限
    * @Param:  roleID: 角色ID
    * @return:
    * @Author: wangze
    */
    @Transactional
    public void removeByRoleId(Integer roleId) {
        rolePermissionRepository.deleteAllByRoleId(roleId);
    }

    /**
    * @Description: 根据角色ID和权限ID删除角色权限表的某一项
    * @Param:  roleId: 角色ID
     * @Param: permissionID: 权限ID
    * @return:
    * @Author: wangze
    */
    @Transactional
    public void deleteRolePermission(Integer roleId, Integer permissionId) {
        rolePermissionRepository.deleteAllByRoleIdAndPermissionId(roleId, permissionId);
    }
}
